﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using DTO;
using DAO;
using System.IO;

namespace BUS
{
    public class CConnect
    {
        CConnectXML _CConnectXML = new CConnectXML();

        #region static

        public static DataTable LayDanhSachKey()
        {
            FileInfo _FileInfo = new FileInfo(BUS.COption.DataPath);
            if (!_FileInfo.Exists)
            {
                BUS.COption.GetPathData();
                return LayDanhSachKey();
            }
            else
                return DAO.CConnectXML.LoadListKeys(BUS.COption.DataPath);
        }

        public static DataTable LayDanhSachKey(string ShortcutKey)
        {
            DataTable _dt = LayDanhSachKey();
            for(int i=0;i<_dt.Rows.Count;i++)
                if (_dt.Rows[i]["Shortcutkey"].ToString().IndexOf(ShortcutKey) == -1)
                    _dt.Rows.RemoveAt(i);
            return _dt;
        }

        public static CKeys LayKey(string Shortcutkey)
        {
            return DAO.CConnectXML.LoadKey(Shortcutkey, BUS.COption.DataPath);
        }

        public static bool ThemKey(String ShortcutKey, string RunningKey, int Delay, int Loop, string Description)
        {
            if (ShortcutKey.Trim()==null)
            {
                MessageBox.Show("Please insert ShortcutKey!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if(!BUS.CCheck.CheckShortcutKey(ShortcutKey.Trim()))
            {
                MessageBox.Show("ShortcutKey is not available! Please insert new ShortcutKey!!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (RunningKey.Trim()==null)
            {
                MessageBox.Show("Please insert RunningKey!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (!BUS.CCheck.CheckRuningKey(RunningKey.Trim()))
            {
                MessageBox.Show("RunningKey error!  Please edit or input new RunningKey!!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (!DAO.CConnectXML.AddKey(new DTO.CKeys(ShortcutKey, RunningKey, Delay, Loop, Description), BUS.COption.DataPath))
            {
                MessageBox.Show("Add fail! Try agan!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            return true;
        }

        public static bool XoaKey(String ShortcutKey)
        {
            if (ShortcutKey.Trim()==null)
            {
                MessageBox.Show("Please insert ShortcutKey!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (!DAO.CConnectXML.DeleteKey(ShortcutKey, BUS.COption.DataPath))
            {
                MessageBox.Show("Delete fail! Try agan!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            return true;
        }

        public static bool XoaKey(CKeys Key)
        {
            return XoaKey(Key.Shortcutkey);
        }

        public static void XoaKeys(DataGridView dataGridView)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow _Row in dataGridView.SelectedRows)
                {
                    if (!XoaKey(_Row.Cells["Shortcutkey"].Value.ToString()))
                    {
                        break;
                    }
                }
            }
        }

        public static bool SuaKey(CKeys KeyNew, String ShortcutKeyOld)
        {
            if (KeyNew.Shortcutkey.Trim() == null)
            {
                MessageBox.Show("Please insert ShortcutKey!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (KeyNew.Shortcutkey.Trim() != ShortcutKeyOld && !BUS.CCheck.CheckShortcutKey(KeyNew.Shortcutkey.Trim()))
            {
                MessageBox.Show("ShortcutKey is not available! Please insert new ShortcutKey!!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (KeyNew.Runningkey.Trim() == null)
            {
                MessageBox.Show("Please insert ShortcutKey!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (!BUS.CCheck.CheckRuningKey(KeyNew.Runningkey.Trim()))
            {
                MessageBox.Show("RunningKey error!  Please edit or input new RunningKey!!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            if (!DAO.CConnectXML.EditKey(KeyNew, ShortcutKeyOld, BUS.COption.DataPath))
            {
                MessageBox.Show("Edit fail! Try agan!", "Warnings", MessageBoxButtons.OK);
                return false;
            }
            return true;
        }

        public static bool SuaKey(CKeys KeyNew, CKeys KeyOld)
        {
            return SuaKey(KeyNew, KeyOld.Shortcutkey);
        }

        public static void Inport()
        {
            OpenFileDialog _Open = new OpenFileDialog();
            _Open.Filter = "List keys File (*.xml)|*.xml|All file (*.*)|*.*";
            if (_Open.ShowDialog() == DialogResult.OK)
            {
                DataTable _Data = DAO.CConnectXML.LoadListKeys(_Open.FileName);
                foreach (DataRow _Row in _Data.Rows)
                {
                    CKeys _Key = new CKeys(_Row["Shortcutkey"].ToString(), _Row["Runningkey"].ToString(), Convert.ToInt32(_Row["Delay"]), Convert.ToInt32(_Row["Loop"]), _Row["Description"].ToString());
                    if (CConnectXML.KiemTraThem(_Key.Shortcutkey, BUS.COption.DataPath))
                    {
                        if (!CConnectXML.AddKey(_Key, BUS.COption.DataPath))
                        {
                            MessageBox.Show("Import error! Try agan!", "Warnings", MessageBoxButtons.OK);
                            break;
                        }
                    }
                    else
                    {
                        if (MessageBox.Show(_Key.Shortcutkey + " is already exists.\nDo you want to replace it?", "Warnings", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            if (!CConnectXML.EditKey(_Key, _Key.Shortcutkey, BUS.COption.DataPath))
                            {
                                MessageBox.Show("Import error! Try agan!", "Warnings", MessageBoxButtons.OK);
                                break;
                            }
                        }
                    }
                }
            }
        }

        public static void Export(DataGridView dataGridView)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                SaveFileDialog _Save = new SaveFileDialog();
                _Save.Filter = "List keys File (*.xml)|*.xml";
                if (_Save.ShowDialog() == DialogResult.OK)
                {
                    CConnectXML.CreateNewTable(_Save.FileName);
                    foreach (DataGridViewRow _Row in dataGridView.SelectedRows)
                    {
                        if (!CConnectXML.AddKey(new CKeys(_Row.Cells["Shortcutkey"].Value.ToString(), _Row.Cells["Runningkey"].Value.ToString(), Convert.ToInt32(_Row.Cells["Delay"].Value), Convert.ToInt32(_Row.Cells["Loop"].Value), _Row.Cells["Description"].Value.ToString()), _Save.FileName))
                        {
                            MessageBox.Show("Export error! Try agan!", "Warnings", MessageBoxButtons.OK);
                            break;
                        }
                    }
                }
            }
        }

        #endregion
    }
}
